import styles from "./index.module.css";
import React from "react";
import type { FormProps } from "antd";
import { Button, Form, Input } from "antd";
import request from "../../axios/request";
import { useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { addshuju } from "../../store/userslick/userslick";
type FieldType = {
  username?: string;
  password?: string;
  emilden?: string;
};
function Index() {
  const nav = useNavigate();
  const dispatch = useDispatch();
  const onFinish: FormProps<FieldType>["onFinish"] = async (values) => {
    console.log("Success:", values);
    await request
      .post("/login", {
        account: values.username,
        password: values.password,
      })
      .then((res) => {
        // console.log(res);
        if (res.data.role == 1) {
          nav("/home");
          dispatch(addshuju(res.data));
          localStorage.setItem("token", res.data.token);
          localStorage.setItem("username", res.data.username);
          localStorage.setItem(
            "url",
            "https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
          );
        }
      });
  };

  const onFinishFailed: FormProps<FieldType>["onFinishFailed"] = (
    errorInfo
  ) => {
    console.log("Failed:", errorInfo);
  };
  return (
    <div className={styles.da}>
      <div className={styles.xiao}>
        <Form
          name="basic"
          labelCol={{ span: 8 }}
          wrapperCol={{ span: 16 }}
          style={{ maxWidth: 600 }}
          initialValues={{ remember: true }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item<FieldType>
            label="账号"
            name="username"
            rules={[{ required: true, message: "输入手机号码!" }]}
          >
            <Input />
          </Form.Item>

          <Form.Item<FieldType>
            label="密码"
            name="password"
            rules={[{ required: true, message: "请输入你的密码!" }]}
          >
            <Input />
          </Form.Item>
          <Form.Item<FieldType>>
            <a>没有账号 ，去注册</a>
          </Form.Item>
          <Form.Item label={null}>
            <Button type="primary" htmlType="submit" block>
              注册
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}

export default Index;
